package com.quan.xczs.web.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.xczs.rpc.api.XczsGoodsGroupGoodsService;
import com.quan.xczs.rpc.api.XczsGoodsGroupService;
import com.quan.xczs.rpc.model.XczsGoodsGroup;

@RestController
@RequestMapping(value = "/api/xczs/goods_group")
public class XczsGoodsGroupController extends BaseController {
	
	@Autowired
	private XczsGoodsGroupService xczsGoodsGroupService;

	@Autowired
	private XczsGoodsGroupGoodsService xczsGoodsGroupGoodsService;
	
	@RequiresPermissions(value="xczs:goods_group:add", description="添加学车招生平台商品套餐")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody XczsGoodsGroup entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.xczsGoodsGroupService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="xczs:goods_group:delete", description="删除学车招生平台商品套餐")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.xczsGoodsGroupService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="xczs:goods_group:delete", description="删除学车招生平台商品套餐")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.xczsGoodsGroupService.deleteByIds(ids) ;
		return BaseResult.ok();
	}

	@RequiresPermissions(value="xczs:goods_group:update", description="修改学车招生平台商品套餐")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody XczsGoodsGroup entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.xczsGoodsGroupService.update(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="xczs:goods_group:detail", description="查询学车招生平台商品套餐详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.xczsGoodsGroupService.selectById(id);
	}
	
	@RequiresPermissions(value="xczs:goods_group:list", description="查询学车招生平台商品套餐")
	@GetMapping(value = "/list")
	public Object list(XczsGoodsGroup entity) {
		return this.xczsGoodsGroupService.selectPage(new Paginator(request), entity) ;
	}
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions(value="xczs:goods_group:add_goods_group", description="添加商品到套餐中")
	@PostMapping(value = "/add_goods_group")
	public Object add_goods_group(@RequestBody Map<Object, Object> params) {
		System.out.println(params);
		Integer goodsGroupId = (Integer) params.get("GoodsGroupId");
		List<Integer> goodsIds = (List<Integer>) params.get("goodsIds");
		
		if(null == goodsGroupId) {
			return BaseResult.failure("套餐不能为空");
		}
		if(null == goodsIds) {
			return BaseResult.failure("商品不能为空");
		}
		
		this.xczsGoodsGroupGoodsService.addGoodsGroupGoods(goodsGroupId, goodsIds);

		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="xczs:goods_group:delete_goods_group", description="删除套餐中关联的商品")
	@GetMapping(value = "/deleteGoodsGroupGoods/{id}")
	public Object deleteGoodsGroupGoods(@PathVariable Integer id) {
		this.xczsGoodsGroupGoodsService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="xczs:goods_group:list_goods_group", description="查询套餐中关联的商品")
	@GetMapping(value = "/list_goods_group")
	public Object listGoodsGroupGoods(Integer goodsGroupId) {
		return this.xczsGoodsGroupGoodsService.selectGoods(goodsGroupId);
	}
}
